local skel = fk.CreateSkill{
  name = "tea__shichong",
}
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["tea__shichong"] = "恃宠",
  [":tea__shichong"] = "一号位的回合结束时，你可以选择以下两项，令一名有手牌的其他角色从其中选择一项：1.将一张牌当你指定的基本牌使用；"..
  "2.将一张你选择的牌当【杀】使用；3.对你指定的目标使用一张牌。",
  ["#tea__shichong-choose"] = "恃宠：选择一名其他角色发动“恃宠”",
  ["shichong_useBasic"] = "①将一张手牌当作 <font color='#00ffff'>%src</font> 指定的基本牌使用",
  ["shichong_useSlash"] = "②将 <font color='#00ffff'>%src</font> 指定的一张手牌当作【杀】使用",
  ["shichong_toTarget"] = "③对 <font color='#00ffff'>%src</font> 指定的一名角色使用一张手牌",
  ["#tea__shichong-choices"] = "恃宠：选择两项并令 %src 选择其中一项",
  ["#tea__shichong-choice"] = "恃宠：选择其中一项",
  ["#tea__shichong-basic"] = "恃宠：选择一个牌名",
  ["#tea__shichong-useSlash"] = "恃宠：选择 %src 的一张牌",
  ["#tea__shichong-choose_use"] = "恃宠：选择一个目标",
  ["#tea__shichong-toTarget"] = "恃宠：对 %src 使用一张牌",
  ["#tea__shichong-invokeBasic"] = "恃宠：将一张牌当【%arg】使用",
  ["#tea__shichong-invokeSlash"] = "恃宠：将这张牌当【杀】使用",

  ["#ShichongBasicCanceled"] = "对 %from 的 %arg 已被取消"
}
---@type TrigSkelSpec<TurnFunc>
local shichong = {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target.seat == 1
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function (p)
      return not p:isNude()
    end)
    if #targets > 0 then
      local tos = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        targets = room:getOtherPlayers(player),
        prompt = "#tea__shichong-choose"
      })
      if #tos == 1 then
        event:setCostData(self, { to = tos[1] })
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to ---@type ServerPlayer
    room:doIndicate(player, { to })
    local all_choices = { "shichong_useBasic:" .. player.id, "shichong_useSlash:" .. player.id, "shichong_toTarget:" .. player.id }
    local choices = room:askToChoices(player, {
      choices = all_choices,
      min_num = 2,
      max_num = 2,
      skill_name = skel.name,
      cancelable = false,
      prompt = "#tea__shichong-choices:" .. to.id
    })
    if #choices == 2 then
      local choice = room:askToChoice(to, {
        choices = choices,
        skill_name = skel.name,
        cancelable = false,
        prompt = "#tea__shichong-choice"
      })
      if not table.contains(all_choices, choice) then return end
      room:notifySkillInvoked(player, skel.name, "control")
      player:broadcastSkillInvoke(skel.name, table.indexOf(all_choices, choice))
      if choice == "shichong_useBasic:" .. player.id then --效果1：转化基本
        local all_names = Fk:getAllCardNames("b", false)
        local names = table.filter(all_names, function (n)
          local card = Fk:cloneCard(n)
          return to:canUse(card)
        end)
        local name = room:askToChoice(player, {
          choices = names,
          all_choices = all_names,
          skill_name = skel.name,
          cancelable = false,
          prompt = "#tea__shichong-basic"
        })
        
        if name then
          if table.find(to:getCardIds("h"), function (id)
            local card = Fk:cloneCard(name)
            card:addSubcard(id)
            ---@diagnostic disable-next-line: return-type-mismatch
            return table.find(room:getAlivePlayers(), function (p)
              return to:canUseTo(card, p)
            end)
          end) then
            local suc, ret = room:askToUseActiveSkill(to, {
              skill_name = "tea__shichong_useBasic",
              cancelable = false,
              prompt = "#tea__shichong-invokeBasic:::" .. name,
              extra_data = {
                interaction_card_names = { name }
              }
            })
            if suc and ret then
              local _card = Fk:cloneCard(ret.interaction)
              _card:addSubcards(ret.cards)
              _card.skillName = "tea__shichong_useBasic"
              local use = { ---@type UseCardDataSpec
                from = to,
                tos = ret.targets,
                card = _card
              }
              room:useCard(use)
            end
          else
            room:sendLog{
              type = "#ShichongBasicCanceled",
              from = to.id,
              arg = skel.name
            }
          end
        end
        

      elseif choice == "shichong_useSlash:" .. player.id then --效果2：转化杀
        local cid = room:askToChooseCard(player, {
          target = to,
          skill_name = skel.name,
          prompt = "#tea__shichong-useSlash:" .. to.id,
          flag = "h",
        })
        if cid then
          local slash = Fk:cloneCard("slash")
          slash:addSubcard(cid)
          if table.find(room:getAlivePlayers(), function (p)
            return to:canUseTo(slash, p)
          end) then
            local suc, ret = room:askToUseActiveSkill(to, {
              skill_name = "tea__shichong_useSlash",
              prompt = "#tea__shichong-invokeSlash",
              cancelable = false,
              extra_data = {
                exclusive_cards = { cid }
              }
            })
            if suc and ret then
              local card = Fk:cloneCard("slash")
              card:addSubcards(ret.cards)
              card.skillName = "tea__shichong_useSlash"
              local use = { ---@type UseCardDataSpec
                from = to,
                tos = ret.targets,
                card = card
              }
              room:useCard(use)
            end
          else
            room:sendLog{
              type = "#ShichongBasicCanceled",
              from = to.id,
              arg = skel.name
            }
          end
        end

      elseif choice == "shichong_toTarget:" .. player.id then --效果3：使用牌
        local tos = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 1,
          skill_name = skel.name,
          targets = room:getAlivePlayers(),
          prompt = "#tea__shichong-choose_use"
        })
        if #tos == 1 then
          local too = tos[1]
          if table.find(to:getCardIds("h"), function (id)
            local card = Fk:getCardById(id)
            return player:canUseTo(card, too, { bypass_distances = true, bypass_times = true })
          end) then
            room:askToUseRealCard(to, {
              pattern = ".|.|.|hand",
              cancelable = false,
              prompt = "#tea__shichong-toTarget:" .. too.id,
              extra_data = {
                exclusive_targets = { too.id },
                bypass_distances = true,
                bypass_times = true,
              }
            })
          else
            room:sendLog{
              type = "#ShichongBasicCanceled",
              from = to.id,
              arg = skel.name
            }
          end
        end
      end
    end
  end
}
skel:addEffect(fk.TurnEnd, shichong)
return skel